import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Random;


public class NoPriorityQuestionProvider implements QuestionProvider {

    // Array of the questions
    private ArrayList<Question> questions;
    
    /**
     *  Array of indexes of questions which weren't asked.
     *  When size of this collection is 0, it's being filled by new integer [from 0 to questions.size()]
     */
    private LinkedList<Integer> indexToAsk;
    private Random random;
    
    public NoPriorityQuestionProvider() {
	questions 	= new ArrayList<Question>();
	indexToAsk 	= new LinkedList<Integer>();
	random 		= GlobalRandom.getRandom();
    }
    
    /* (non-Javadoc)
     * @see QuestionProvider#addQuestion(Question)
     */
    @Override
    public void addQuestion(Question question) {
	questions.add(question);
	indexToAsk.add(questions.size());
    }
    
    /* (non-Javadoc)
     * @see QuestionProvider#addQuestion(java.lang.String)
     */
    @Override
    public void addQuestion(String question) {
	Question temp = new Question(question);
	questions.add(temp);
	indexToAsk.add(questions.size() - 1);
    }
    
    /* (non-Javadoc)
     * @see QuestionProvider#getQuestion()
     */
    @Override
    public Question nextQuestion() {
	int resultIndex;
	if (indexToAsk.size() == 0) {
	    for (int i = 0; i < questions.size(); i++) {
		indexToAsk.add(i);
	    }
	    return new Question("loop is complited");
	}
	int tempIndex = random.nextInt(indexToAsk.size());
	resultIndex = indexToAsk.get(tempIndex);
	indexToAsk.remove(tempIndex); 
	return questions.get(resultIndex);
    }
}
